#!/usr/bin/python2.5
#
# Copyright (c) 2009 Digital Darkness
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Model representing a wave."""

__author__ = 'omni@digitaldarkness.com (Omni Adams)'

import base64
from google.appengine.ext import db


class Wave(db.Model):
  """Wave object."""
 
  # ID of the individual blip.
  blip_id = db.StringProperty()

  # ID of the whole wave.
  wave_id = db.StringProperty()

  # Title of the whole wave (first line of the initial blip.
  title = db.StringProperty()

  # Email of the creator of the blip.
  creator = db.StringProperty()

  # Text content of the blip.
  content = db.TextProperty()

  def __str__(self):
    """Return the wave as a serialized PHP string.

    The content field is Base64 encoded.
    Returns:
      A PHP serialized object representing the wave.
    """
    class_length = 4
    num_properties = 5
    content = self.content.encode('utf-8')
    content = base64.b64encode(content)
    string = ('O:%s:"Wave":%s:{'
              's:13:"\0Wave\0wave_id";'
              's:%s:"%s";'
              's:13:"\0Wave\0blip_id";'
              's:%s:"%s";'
              's:11:"\0Wave\0title";'
              's:%s:"%s";'
              's:13:"\0Wave\0content";'
              's:%s:"%s";'
              's:13:"\0Wave\0creator";'
              's:%s:"%s";'
              '}') % (class_length, num_properties,
                      len(self.wave_id), self.wave_id,
                      len(self.blip_id), self.blip_id,
                      len(self.title), self.title,
                      len(content), content,
                      len(self.creator), self.creator)
    return string.encode('utf-8')
